package com.enwei.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.enwei.utils.Constans;
import com.ew.action.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;


public class VideoListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private JSONArray arrays = null;
    private Context mContext;
    public VideoListAdapter(JSONArray arrays, Context context){
        this.arrays = arrays;
        this.mContext  = context;
        inflater = LayoutInflater.from(context);
    }



    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.view_video_list_cell, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        onBindViewHolder(holder,position);
        return convertView;
    }

    @Override
    public int getCount() {
        return arrays.length();
    }

    @Override
    public Object getItem(int i) {
        try {
			return arrays.get(i);
		} catch (JSONException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    private class ViewHolder {
        public TextView title;
        public ImageView imgurl;

        public ViewHolder(View view) {
        	title = (TextView) view.findViewById(R.id.title);
        	imgurl = (ImageView) view.findViewById(R.id.imgurl);
        }
    }

    private void onBindViewHolder(ViewHolder holder, int position){
    	try {
    		JSONObject obj = (JSONObject) getItem(position);
    		holder.title.setText(obj.getString("title"));
    		BitmapUtils bitmapUtils = new BitmapUtils(mContext);
    		BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
    		displayConfig.setLoadingDrawable(mContext.getResources().getDrawable(
    				R.drawable.default_image));
    		displayConfig.setLoadFailedDrawable(mContext.getResources().getDrawable(
    				R.drawable.default_image));
    		bitmapUtils.display(holder.imgurl, Constans.IMAGE_URL+obj.getString("imgurl"),displayConfig);

		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void updateView(JSONArray arrays){
    	this.arrays = arrays;
    	notifyDataSetChanged();
    }

}
